package com.ml.doctor.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ml.doctor.R;
import com.ml.doctor.bean.UserInfoBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.QiNiuUtils;
import com.ml.doctor.view.BasicItem;
import com.ml.doctor.view.CycleImageView;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class UserInfoActivity extends BaseActivity {

    public static final int REFRESH_CODE = 111;
    public static final int UPDATE_IM_CODE = 2;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.person_image)
    CycleImageView personImage;
    @BindView(R.id.s_more)
    ImageView sMore;
    @BindView(R.id.person_image_layout)
    RelativeLayout personImageLayout;
    @BindView(R.id.bi_name)
    BasicItem biName;
    @BindView(R.id.bi_zhiwu)
    BasicItem biZhiwu;
    @BindView(R.id.bi_keshi)
    BasicItem biKeshi;
    @BindView(R.id.bi_yiyuan)
    BasicItem biYiyuan;
    @BindView(R.id.bi_zhicheng)
    BasicItem biZhicheng;
    UserInfoBean userInfoBean;
    @BindView(R.id.bi_qyfy)
    BasicItem biQyfy;
    @BindView(R.id.bi_fwfy)
    BasicItem biFwfy;
    @BindView(R.id.bi_gerensc)
    BasicItem biGerensc;
    @BindView(R.id.bi_grjj)
    BasicItem biGrjj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        //showLoadingDialog();
        NetworkApi.getUserInfo(listener);
        setTopTitle("个人资料");
    }

    public NetworkManager.SuccessCallback<UserInfoBean> listener = new NetworkManager.SuccessCallback<UserInfoBean>() {
        @Override
        public void onSuccess(UserInfoBean response) {
            if (response == null) {
                return;
            }
            userInfoBean = response;
            setupView(response);
        }
    };

    private void setupView(UserInfoBean response) {
        Glide.with(mContext).load(response.docter_photo).asBitmap().into(personImage);
        biName.setSubTitle(response.doctername);
        biKeshi.setSubTitle(response.department);
        biYiyuan.setSubTitle(response.hosname);
        biZhiwu.setSubTitle(response.duty);
        biZhicheng.setSubTitle(response.rankname);
        biQyfy.setSubTitle(response.apply_amount + "");
        biFwfy.setSubTitle(response.service_amount + "");
        biGerensc.setSubTitle(response.gat + "");
        biGrjj.setSubTitle(response.pro + "");
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @OnClick({R.id.person_image, R.id.bi_name, R.id.bi_zhiwu, R.id.bi_keshi, R.id.bi_yiyuan,
            R.id.bi_qyfy, R.id.bi_fwfy, R.id.bi_gerensc, R.id.bi_grjj})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.person_image:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    checkPermission(UPDATE_IM_CODE);
                } else {
                    selectPicture(UPDATE_IM_CODE);
                }
                break;
            case R.id.bi_name:
                startEditActivity("doctername");
                break;
            case R.id.bi_zhiwu:
                startEditActivity("duty");
                break;
            case R.id.bi_keshi:
                startEditActivity("department");
                break;
            case R.id.bi_yiyuan:
                startEditActivity("hosname");
                break;
            case R.id.bi_qyfy:
                startEditActivity("apply_amount");
                break;
            case R.id.bi_fwfy:
                startEditActivity("service_amount");
                break;
            case R.id.bi_gerensc:
                startEditActivity("gat");
                break;
            case R.id.bi_grjj:
                startEditActivity("pro");
                break;
        }
    }

    private void startEditActivity(String type) {
        Intent i = new Intent(this, EditInfoActivity.class);
        i.putExtra("type", type);
        i.putExtra("info", userInfoBean);
        startActivityForResult(i, REFRESH_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REFRESH_CODE && resultCode == RESULT_OK) {
            UserInfoBean userInfoBean = (UserInfoBean) data.getSerializableExtra("response");
            setupView(userInfoBean);
        }
        if (requestCode == UPDATE_IM_CODE && resultCode == RESULT_OK) {
            final List<String> list = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            Glide.with(this).load(list.get(0)).asBitmap().into(personImage);
            NetworkApi.getQiNiuToken(new NetworkManager.SuccessCallback<String>() {
                @Override
                public void onSuccess(String response) {
                    if (TextUtils.isEmpty(response)){
                        return;
                    }
                    QiNiuUtils.getInstance().uploadImage(response, list.get(0), new UpCompletionHandler() {
                        @Override
                        public void complete(String key, ResponseInfo info, JSONObject response) {
                            NetworkApi.updateAvatar("http://oyptcv2pb.bkt.clouddn.com/" + key, new NetworkManager.SuccessCallback<UserInfoBean>() {
                                @Override
                                public void onSuccess(UserInfoBean response) {

                                }
                            });
                        }
                    });
                }
            });
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermission(int requestCode) {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        } else {
            selectPicture(requestCode);
        }
    }

    private void selectPicture(int requestCode) {
        Intent intent = new Intent(this, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, requestCode);
    }


}
